﻿using System.Collections.Generic;
using Cms.Application.Admin.Views;
using Cms.Data;
using Cms.Domain;
using Cms.Domain.RepositoryInterfaces;

namespace Cms.Application.Admin.Presenters
{
    public class UsersPresenter : BasePresenter
    {
        private readonly Administrator _administrator;
        private readonly IUsersView _view;

        public UsersPresenter(IUsersView view)
            : this(view, new RepositoryFactory())
        {
        }

        public UsersPresenter(IUsersView view, IRepositoryFactory repositoryFactory)
            : base(view, repositoryFactory)
        {
            _view = view;
            _administrator = AdministratorRepository.GetAdministrator(_view.AdministratorID);
            if (!_administrator.HasPermission.Users)
            {
                _view.RedirectToPermissionDeniedPage();
            }
        }

        public void Initialize()
        {
            SetUserGroups();
            _view.SelectedUserGroupId = -1;
            SearchUsers();
        }

        public void SetUserGroups()
        {
            _view.SetUserGroups(UserRepository.GetUserGroups());
        }

        public void SearchUsers()
        {
            IList<User> users = UserRepository.SearchUsers();
            _view.SetUsers(users);
        }

        public void DeleteUser(int id)
        {
            UserRepository.DeleteUser(id);
            SearchUsers();
        }
    }
}
